﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.Practices.Unity;

namespace BootCms.Environment
{
    public class PerRequestLifetimeManager : LifetimeManager
    {
        readonly object key = new object();

        public override object GetValue()
        {
            IDictionary items = GetItems();

            if (items != null && items.Contains(this.key))
                return items[this.key];

            return null;
        }

        public override void RemoveValue()
        {

            IDictionary items = GetItems();

            if (items != null)
                items.Remove(this.key);
        }

        public override void SetValue(object newValue)
        {

            IDictionary items = GetItems();

            if (items != null)
                items[this.key] = newValue;
        }

        IDictionary GetItems()
        {

            HttpContext context = HttpContext.Current;

            if (context != null)
                return context.Items;

            return null;
        }
    }

}